package com.geeke.sys.controller;

import com.alibaba.fastjson.JSONObject;

import com.geeke.common.controller.SearchParams;
import com.geeke.common.data.Page;
import com.geeke.dc.entity.DcCondition;
import com.geeke.org.entity.Company;
import com.geeke.sys.controller.BaseController;
import com.geeke.sys.entity.CodeRule;
import com.geeke.sys.entity.CompanyCodeRule;
import com.geeke.sys.service.CodeRuleService;
import com.geeke.sys.service.CompanyCodeRuleService;
import com.geeke.utils.ResultUtil;
import com.geeke.utils.StringUtils;
import com.google.common.collect.Lists;
import java.util.List;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公司编码规则Controller
 * @author
 * @version
 */
@Api(value = "/sys/companyCodeRule", tags = {"公司编码规则Controller"})
@RestController
@RequestMapping(value = "/sys/companyCodeRule")
public class CompanyCodeRuleController extends BaseController {

    @Autowired
    private CompanyCodeRuleService companyCodeRuleService;

    @Autowired
    private CodeRuleService codeRuleService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "获取公司编码规则", notes = "通过id获取公司编码规则", httpMethod = "GET",response = CompanyCodeRule.class)
    @GetMapping("/{id}")
    public ResponseEntity<JSONObject> getById(@PathVariable("id") String id) {
        CompanyCodeRule entity = companyCodeRuleService.get(id);

        return ResponseEntity.ok(ResultUtil.successJson(entity));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SearchParams", name = "searchParams", value = "", required = true)
    })
    @ApiOperation(value = "（分页）通过条件获取公司编码规则", notes = "通过条件分页获取公司编码规则", httpMethod = "POST",response =CompanyCodeRule.class)
    @PostMapping(value = { "list", "" })
    public ResponseEntity<JSONObject> listPage(@RequestBody SearchParams searchParams) {
        Page<CompanyCodeRule> result = companyCodeRuleService.listPage(
            searchParams.getParams(),
            searchParams.getOffset(),
            searchParams.getLimit(),
            searchParams.getOrderby()
        );

        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SearchParams", name = "searchParams", value = "", required = true)
    })
    @ApiOperation(value = "通过条件获取公司编码规则", notes = "通过条件获取公司编码规则", httpMethod = "POST",response = CompanyCodeRule.class,responseContainer  ="List")
    @PostMapping(value = "listAll")
    public ResponseEntity<JSONObject> listAll(@RequestBody SearchParams searchParams) {
        List<CompanyCodeRule> result = companyCodeRuleService.listAll(searchParams.getParams(), searchParams.getOrderby());

        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "CompanyCodeRule", name = "entity", value = "", required = true)
    })
    @ApiOperation(value = "保存公司编码规则", notes = "保存公司编码规则，并且返回id", httpMethod = "POST",response = String.class)

    @PostMapping(value = "save")
    public ResponseEntity<JSONObject> save(@RequestBody CompanyCodeRule entity) {
        String id = companyCodeRuleService.save(entity).getId();
        return ResponseEntity.ok(ResultUtil.successJson(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "CompanyCodeRule", name = "entity", value = "", required = true)
    })
    @ApiOperation(value = "删除公司编码规则", notes = "删除公司编码规则,并且返回删除条数", httpMethod = "POST",response = Integer.class)
    @PostMapping(value = "delete")
    public ResponseEntity<JSONObject> delete(@RequestBody CompanyCodeRule entity) {
        int rows = companyCodeRuleService.delete(entity);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }


    @ApiOperation(value = "批量添加公司编码规则", notes = "批量添加公司编码规则，并且返回ids（list）", httpMethod = "POST",response = List.class)
    @PostMapping(value = "bulkInsert")
    public ResponseEntity<JSONObject> bulkInsert(@RequestBody @ApiParam() List<CompanyCodeRule> entitys) {
        List<String> ids = companyCodeRuleService.bulkInsert(entitys);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }


    @ApiOperation(value = "批量更新公司编码规则", notes = "批量更新公司编码规则，并且返回ids（list）", httpMethod = "POST",response = List.class)
    @PostMapping(value = "bulkUpdate")
    public ResponseEntity<JSONObject> bulkUpdate(@RequestBody @ApiParam() List<CompanyCodeRule> entitys) {
        List<String> ids = companyCodeRuleService.bulkUpdate(entitys);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }


    @ApiOperation(value = "批量删除公司编码规则", notes = "批量删除公司编码规则，并且返回删除条数", httpMethod = "POST",response = Integer.class)
    @PostMapping(value = "bulkDelete")
    public ResponseEntity<JSONObject> bulkDelete(@RequestBody @ApiParam() List<CompanyCodeRule> entitys) {
        int rows = companyCodeRuleService.bulkDelete(entitys);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    private void initCompanyCodeRule(String companyId) {
        List<CodeRule> rules = codeRuleService.listNotInCompany(companyId);
        List<CompanyCodeRule> ccrs = Lists.newArrayList();
        for (CodeRule cr : rules) {
            Company company = new Company();
            company.setId(companyId);

            CompanyCodeRule ccr = new CompanyCodeRule();
            ccr.setCompany(company);
            ccr.setSysRule(cr);
            ccr.setCode(cr.getCode());
            ccr.setName(cr.getName());
            ccr.setRuleDef(cr.getRuleDef());
            ccr.setRemarks(cr.getRemarks());
            ccrs.add(ccr);
            companyCodeRuleService.save(ccr);
        }
        if (ccrs.size() > 0) {
            companyCodeRuleService.bulkInsert(ccrs);
        }
    }
}
